package home.my.recordmanage.web;

import home.my.base.annotation.SingleValue;
import home.my.base.sql.AbsCondition;
import home.my.base.sql.ConditionQuery;
import home.my.base.sql.NoValueCondition;
import home.my.base.sql.SingleValueCondition;
import home.my.base.util.DateUtil;
import home.my.base.util.downloader.CsvDownLoader;
import home.my.base.web.ModelAction;
import home.my.recordmanage.model.DealIdea;
import home.my.recordmanage.model.Record;
import home.my.recordmanage.service.DealIdeaService;
import home.my.recordmanage.service.RecordService;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.json.annotations.JSON;

import com.opensymphony.xwork2.Action;

public class RecordAction extends ModelAction<Record,RecordService>{
	private String beginDate;
	private String endDate;
	private String schStatus;
	private String schCompany;
	private String schSubmitPerson;
	private String schIsBad;
	private List<Map<String, Object>> statList;
	
	private String needsFinishDate;
	private String dealIdea;
	
	private DealIdeaService dealIdeaService;
	
	@Override
	public ConditionQuery buildQuery(){
		ConditionQuery q = super.buildQuery();
		if(StringUtils.isNotEmpty(schStatus)){
			int status = Integer.parseInt(schStatus);
			AbsCondition c1;
			if(status>=0){
				c1 = new SingleValueCondition("status",status);
				q.add(c1);
			}else if(status==STATUS.all.value){
				//查全部，不需要条件
			}else if(status==-2){
				c1 = new NoValueCondition(" t.status=1 or t.status=2 ");
				q.add(c1);
			}
		}
		if(StringUtils.isNotEmpty(schIsBad) && Integer.parseInt(schIsBad)>-1){
			AbsCondition c2= new SingleValueCondition("isBad",Integer.parseInt(schIsBad));
			q.add(c2);
		}
		q.setOrders("id desc");
		return q;
	}
	
	@Override
	public String create() {
		this.getService().createRecord(super.getPojo(),dealIdea);
		return Action.SUCCESS;
	}
	
	@Override
	public String update() {
		this.getService().updateRecord(this.getPojo(),dealIdea,needsFinishDate);
		return Action.SUCCESS;
	}

	/******************************
	 * @author ning.ma
	 * 创建日期: 2012-6-27 下午02:39:29<br/>
	 * 功能：统计<br/>
	 * 参数：@return<br/>
	 * 返回类型：String<br/>
	*****************************************/
	public String stat(){
		statList = this.getService().statRecord(DateUtil.parseStrToDate(beginDate), DateUtil.parseStrToDate(endDate));
		return Action.SUCCESS;
	}
	
	/* (non-Javadoc)
	 * @see home.my.base.web.SearchAction#exportCsv()
	 */
	public void exportCsv(){
//		jetty不用转码，tomcat就需要
//		System.out.print(schCompany);
//		try {
//			schCompany = StringUtils.isNotEmpty(schCompany)?new String(schCompany.getBytes("ISO8859_1"),"UTF-8"):null;
//			schSubmitPerson = StringUtils.isNotEmpty(schSubmitPerson)?new String(schSubmitPerson.getBytes("ISO8859_1"),"UTF-8"):null;
//		} catch (UnsupportedEncodingException e) {
//			e.printStackTrace();
//		}
//		System.out.print(schCompany);
		this.setDownloader(new CsvDownLoader<Record>(this.getResponse()){

			@Override
			public String buildTitle() {
				return "录入时间"+SPLIT+"提交人"+SPLIT+"来源"+SPLIT+"故障类型"+SPLIT+"公司"+SPLIT+"联系人"+SPLIT+"电话"+SPLIT+"URL"+SPLIT+"故障"+SPLIT+"状态"+SPLIT+"合理性"+SPLIT+"完成时间"+SPLIT+"处理意见";
			}

			@Override
			public String buildCsvRow(Record r) {
				StringBuilder sb = new StringBuilder();
				sb.append( replaceChars( r.getReceieveDateStr() ) ).append(SPLIT);
				sb.append( replaceChars( r.getSubmitPerson() ) ).append(SPLIT);
				sb.append( replaceChars( r.getSource().getName() ) ).append(SPLIT);
				sb.append( replaceChars( r.getErrorType().getName() ) ).append(SPLIT);
				sb.append( replaceChars( r.getCompany() ) ).append(SPLIT);
				sb.append( replaceChars( r.getContact() ) ).append(SPLIT);
				sb.append( replaceChars( r.getPhone() ) ).append(SPLIT);
				sb.append( replaceChars( r.getLinkUrl() ) ).append(SPLIT);
				sb.append( replaceChars( r.getProblemDesc() ) ).append(SPLIT);
				sb.append( replaceChars( r.getFmtStatus() ) ).append(SPLIT);
				sb.append( replaceChars( r.getFmtIsBad() ) ).append(SPLIT);
				sb.append( replaceChars( r.getFinishDateStr() ) );
				
				List<DealIdea> dealIdeaList = dealIdeaService.find("ID", false, new SingleValueCondition("recordFk",r.getId()));
				if(dealIdeaList!=null && dealIdeaList.size()>0){
					sb.append(SPLIT);
					StringBuilder d_sb = new StringBuilder();
					for(DealIdea d:dealIdeaList){
						d_sb.append( d.getIdeaDateStr() ).append("\r\n").append( d.getIdea() ).append("\r\n\r\n");
					}
					sb.append( replaceChars( d_sb.toString() ) );
				}
				
				return sb.toString();
			}
			
		});
		super.exportCsv();
	}
	
	/******************************
	 * @author ning.ma
	 * 创建日期: 2012-9-11 下午2:31:41<br/>
	 * 功能：修复单据入口，更新0日期的单据<br/>
	 * 参数：@return<br/>
	 * 返回类型：String<br/>
	*****************************************/
	public String fix(){
		statList = this.getService().fixErrorRecords();
		return Action.SUCCESS;
	}
	
	@SingleValue(column="receieveDate",equal=">=")
	@JSON(serialize=false)
	public Long getBeginDate() {
		java.util.Date date = DateUtil.parseStrToDate(beginDate);
		return date==null?null:date.getTime();
	}
	
	@SingleValue(column="receieveDate",equal="<=")
	@JSON(serialize=false)
	public Long getEndDate() {
		java.util.Date date = DateUtil.maxlizedTime(DateUtil.parseStrToDate(endDate));
		return date==null?null:date.getTime();
	}
	
	@SingleValue(equal="like")
	@JSON(serialize=false)
	public String getSchCompany() {
		return StringUtils.isNotEmpty(schCompany)?"%"+schCompany+"%":null;
	}
	
	@SingleValue(equal="like")
	@JSON(serialize=false)
	public String getSchSubmitPerson() {
		return StringUtils.isNotEmpty(schSubmitPerson)?"%"+schSubmitPerson+"%":null;
	}
	@JSON(serialize=false)
	public String getSchStatus() {
		return schStatus;
	}
	public void setBeginDate(String beginDate) {
		this.beginDate = beginDate;
	}
	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}
	public void setSchStatus(String schStatus) {
		this.schStatus = schStatus;
	}
	public void setSchCompany(String schCompany) {
		this.schCompany = schCompany;
	}
	public void setSchSubmitPerson(String schSubmitPerson) {
		this.schSubmitPerson = schSubmitPerson;
	}
	public String getSchIsBad() {
		return schIsBad;
	}
	public void setSchIsBad(String schIsBad) {
		this.schIsBad = schIsBad;
	}
	
	public List<Map<String, Object>> getStatList() {
		return statList;
	}

	public void setNeedsFinishDate(String needsFinishDate) {
		this.needsFinishDate = needsFinishDate;
	}

	public void setDealIdea(String dealIdea) {
		this.dealIdea = dealIdea;
	}

	public void setDealIdeaService(DealIdeaService dealIdeaService) {
		this.dealIdeaService = dealIdeaService;
	}
}
